package com.zcg.blogger.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.zcg.blogger.exception.ArticleException;
import com.zcg.blogger.exception.UserException;
import com.zcg.blogger.model.Article;
import com.zcg.blogger.model.CSDNArticle;
import com.zcg.blogger.model.Category;
import com.zcg.blogger.model.Comment;
import com.zcg.blogger.model.User;
import com.zcg.blogger.service.ArticleService;
import com.zcg.blogger.service.CSDNService;

/**
 * 文章控制器
 * 
 * @author zcg
 *
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

	@Resource
	private ArticleService articleService;
	
	@Resource
	private CSDNService csdnService;

	/**
	 * 展示文章
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/showArticle")
	public String toIndex(HttpServletRequest request, Model model) {
		int id = Integer.parseInt(request.getParameter("id"));
		Article article = articleService.loadById(id);
		model.addAttribute("user", article);
		return "hello";
	}


	/**
	 * 文章列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/articles","/"}, method = RequestMethod.GET)
	public String list(Model model, HttpSession session) {
		List<Article> articles = articleService.loadArticleAll();
		List<Article> hotArticles = articleService.loadArticleHot();
		List<CSDNArticle> csdnArticles = csdnService.findTop10();
		List<Category> categorys = articleService.loadArticleCategorys();
		model.addAttribute("articles", articles);
		session.setAttribute("categorys", categorys);
		session.setAttribute("hotArticles", hotArticles);
		session.setAttribute("csdnArticles", csdnArticles);
		model.addAttribute("url", "article/articles");
		return "article/index";
	}

	/**
	 * 根据类别展示的文章列表
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/list", method = RequestMethod.GET)
	public String list(@PathVariable int id, Model model,HttpSession session) {
		List<Article> articles = articleService.loadArticlesByCategoryId(id);
		Category category = articleService.loadByCategoryId(id);
		model.addAttribute("articles", articles);
		model.addAttribute("category", category);
		model.addAttribute("url", "article/"+id+"/list");
		return "article/index";
	}

	/**
	 * 添加文章页面
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(@ModelAttribute("article") Article article, Model model) {
		List<Category> categorys = articleService.loadArticleCategorys();
		model.addAttribute("categorys", categorys);
		return "article/add";
	}

	/**
	 * 增加一篇新文章
	 * @param article
	 * @param br
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@Validated Article article, BindingResult br, Model model) {
		if(article.getUser().getId() == 0){
			throw new UserException("请先登录");
		}
		if (br.hasErrors()) {
			return "article/add";
		}
		articleService.add(article);
		model.addAttribute(article);
		return "redirect:"+article.getId()+"/show";
	}

	/**
	 * 根据id显示文章
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/show", method = RequestMethod.GET)
	public String show(@PathVariable int id, Model model) {
		Article article = articleService.loadById(id);
		List<Comment> comments = articleService.loadCommentByArticleId(id);
		int commentCount = articleService.loadCommentCountByArticleId(id);
		model.addAttribute("commentCount", commentCount);
		model.addAttribute("article", article);
		model.addAttribute("comments", comments);
		model.addAttribute("url", "article/"+id+"/show");
		return "article/show";
	}
	
	/**
	 * 根据当前id跳转到下一篇文章
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/next", method = RequestMethod.GET)
	public String next(@PathVariable int id, Model model){
		Article article = articleService.loadNextById(id);
		if(article == null){
			article = articleService.loadById(id);
		}
		List<Comment> comments = articleService.loadCommentByArticleId(article.getId());
		int commentCount = articleService.loadCommentCountByArticleId(article.getId());
		model.addAttribute("commentCount", commentCount);
		model.addAttribute("article", article);
		model.addAttribute("comments", comments);
		model.addAttribute("url", "article/"+id+"/next");
		return "article/show";
	}
	
	/**
	 * 根据当前id跳转到上一篇文章
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/previous", method = RequestMethod.GET)
	public String previous(@PathVariable int id, Model model){
		Article article = articleService.loadPreviousById(id);
		if(article == null){
			article = articleService.loadById(id);
		}
		List<Comment> comments = articleService.loadCommentByArticleId(article.getId());
		int commentCount = articleService.loadCommentCountByArticleId(article.getId());
		model.addAttribute("commentCount", commentCount);
		model.addAttribute("article", article);
		model.addAttribute("comments", comments);
		model.addAttribute("url", "article/"+id+"/previous");
		return "article/show";
	}

	/**
	 * 根据id显示文章json
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET, params = "json")
	@ResponseBody
	public Article show(@PathVariable int id) {
		return articleService.loadById(id);
	}

	/**
	 * 评论文章或回复
	 * @param comment
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/comment", method = RequestMethod.POST)
	public String comment(Comment comment, Model model) {
		if(comment.getUser().getId() == 0){
			throw new UserException("请先登录");
		}
		if(comment.getUser().getId() == comment.getCommentUser().getId()){
			throw new UserException("自己不可以给自己评论");
		}
		
		articleService.addComment(comment);
		return "redirect:"+comment.getArticle().getId()+"/show";
	}
	
	/**
	 * 根据文章id点赞
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/praise/{id}",method = RequestMethod.GET)
	public String addPraise(@PathVariable int id){
		articleService.addPraise(id);
		return "true";
	}

	/**
	 * 给评论点赞
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/comment/praise/{id}",method = RequestMethod.GET)
	public String addCommentPraise(@PathVariable int id){
		articleService.addCommentPraise(id);
		return "true";
	}

	/**
	 * 根据id更新文章
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String update(@PathVariable int id, Model model) {
		// public String add(Model model){
		Article user = articleService.loadById(id);
		model.addAttribute(user);
		return "user/update";
	}

	/**
	 * 根据id更新文章
	 * 
	 * @param id
	 * @param user
	 * @param br
	 * @return
	 */
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(@PathVariable int id, @Validated Article article,
			BindingResult br) {
		if (br.hasErrors()) {
			// 如果有错误返回add页面
			return "user/update";
		}

		articleService.update(article);
		return "redirect:../user/users";
	}

	/**
	 * 根据id删除文章
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable int id) {
		Article user = articleService.loadById(id);
		articleService.delete(user);
		return "redirect:../users";
	}

}